﻿using MediatR;
using ProduceBasicData.Api.Application.Command.WarehouseManage;
using ProduceBasicData.Domain.WarehouseManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.WarehouseManage
{
    /// <summary>
    /// 仓库库区下拉框命令处理器
    /// </summary>
    public class GetWarehouseZoneDropdownCommandHandler : IRequestHandler<GetWarehouseZoneDropdownCommand, ApiResult<List<WarehouseZone>>>
    {
        private readonly IBaseRespository<WarehouseZone> _warehouseZoneRepository;

        public GetWarehouseZoneDropdownCommandHandler(IBaseRespository<WarehouseZone> warehouseZoneRepository)
        {
            _warehouseZoneRepository = warehouseZoneRepository;
        }

        public Task<ApiResult<List<WarehouseZone>>> Handle(GetWarehouseZoneDropdownCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<List<WarehouseZone>>()
            {
                Code = ApiEnum.Success,
                Msg = "仓库库区查询成功"
            };
            
            // 查询指定仓库ID的未删除库区数据
            var warehouseZones = _warehouseZoneRepository.GetAll()
                .Where(x => x.WarehouseId == request.WarehouseId && !x.Isdel)
                .ToList();

            result.Data = warehouseZones;

            return Task.FromResult(result);
        }
    }
}